package com.xxx.reggie.controller;

import com.xxx.reggie.common.R;
import com.xxx.reggie.dto.DishDto;
import com.xxx.reggie.entity.Dish;
import com.xxx.reggie.entity.Employee;
import com.xxx.reggie.entity.Page;
import com.xxx.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {

    //注入DishService
    @Autowired(required = false)
    private DishService dishService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session){

        //完善数据(修改人,创建人)
        Employee employee = (Employee) session.getAttribute("employee");
        //创建人
        dishDto.setCreateUser(employee.getId());
        //修改人
        dishDto.setUpdateUser(employee.getId());

        //调用业务实现添加操作
        dishService.save(dishDto);

        //返回封装结果
        return R.success("添加成功");
    }


    @GetMapping("/page")
    public R<Page<DishDto>> findByPage(Integer page, Integer pageSize){

        Page<DishDto> byPage = dishService.findByPage(page, pageSize);

        return R.success(byPage);
    }

    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable long id){
        DishDto byId = dishService.findById(id);

        return R.success(byId);
    }

    @PutMapping
    public R<String> updateById(@RequestBody DishDto dishDto,HttpSession session){
        //获取登入用户数据
        Employee employee = (Employee) session.getAttribute("employee");

        //更新修改人
        dishDto.setUpdateUser(employee.getId());

        //调用业务修改数据
        dishService.updateById(dishDto);

        //返回结果
        return R.success("修改成功");
    }


    @GetMapping("/list")
    public R<List<DishDto>> findByCid(Long categoryId,@RequestParam(defaultValue = "1") Integer status){
        //接收类别Id
        //调用业务根据类别id查询菜品列表
        List<DishDto> dishDtoList = dishService.findAllByCid(categoryId,status);

        //封装数据返回
        return R.success(dishDtoList);
    }


    @DeleteMapping
    public R<String> deleteById(@RequestParam List<Long> ids){

        dishService.deleteById(ids);

        return R.success("删除成功");
    }


    @PostMapping("/status/{status}")
    public R<String> findByDishId(@RequestParam List<Long> ids ,@PathVariable Integer status){
        //接收菜品类别Id
        //调用业务根据类别id修改菜品状态
        dishService.findByDishId(ids,status);

        //返回结果
        return R.success("修改成功");
    }
}
